<!doctype html>
<html lang="zh-Hans" dir="ltr" class="docs-wrapper docs-doc-page docs-version-current plugin-docs plugin-id-default docs-doc-id-action/springfox/springfox8">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Docusaurus v2.0.1">
<link rel="search" type="application/opensearchdescription+xml" title="Knife4j" href="/opensearch.xml">

<!-- Google Tag Manager -->
    <script>!function(e,t,a,n,g){e[n]=e[n]||[],e[n].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var m=t.getElementsByTagName(a)[0],r=t.createElement(a);r.async=!0,r.src="https://www.googletagmanager.com/gtm.js?id=GTM-TKBX678",m.parentNode.insertBefore(r,m)}(window,document,"script","dataLayer")</script>
    <!-- End Google Tag Manager -->
<script src="/js/custom.js"></script>
<script src="/js/baidu.js"></script>
<script src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1589206801610969" async crossorigin="anonymous"></script><title data-rh="true">springfox 源码分析(八) 遍历接口获取Model对象 | Knife4j</title><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="https://doc.xiaominfo.com/docs/action/springfox/springfox8"><meta data-rh="true" name="docusaurus_locale" content="zh-Hans"><meta data-rh="true" name="docsearch:language" content="zh-Hans"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="springfox 源码分析(八) 遍历接口获取Model对象 | Knife4j"><meta data-rh="true" name="description" content="我们通过读DocumentationPluginsBootstrapper代码中的start方法,了解到springfox根据我们外部提供的Docket对象进行初始化时,会通过Docket对象构建DocumentationContext对象来进行初始化操作"><meta data-rh="true" property="og:description" content="我们通过读DocumentationPluginsBootstrapper代码中的start方法,了解到springfox根据我们外部提供的Docket对象进行初始化时,会通过Docket对象构建DocumentationContext对象来进行初始化操作"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://doc.xiaominfo.com/docs/action/springfox/springfox8"><link data-rh="true" rel="alternate" href="https://doc.xiaominfo.com/docs/action/springfox/springfox8" hreflang="zh-Hans"><link data-rh="true" rel="alternate" href="https://doc.xiaominfo.com/docs/action/springfox/springfox8" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://3CRIMRK623-dsn.algolia.net" crossorigin="anonymous"><link rel="stylesheet" href="/assets/css/styles.66359a73.css">
<link rel="preload" href="/assets/js/runtime~main.e0db172c.js" as="script">
<link rel="preload" href="/assets/js/main.c059fb6e.js" as="script">
</head>
<body class="navigation-with-keyboard">
<script>!function(){function e(e){document.documentElement.setAttribute("data-theme",e)}var t=function(){var e=null;try{e=localStorage.getItem("theme")}catch(e){}return e}();null!==t?e(t):window.matchMedia("(prefers-color-scheme: dark)").matches?e("dark"):window.matchMedia("(prefers-color-scheme: light)").matches?e("light"):e("dark")}()</script><div id="__docusaurus">
<div role="region"><a href="#" class="skipToContent_fXgn">跳到主要内容</a></div><nav class="navbar navbar--fixed-top navbarHideable_m1mJ"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/knife4j-light.svg" alt="" class="themedImage_ToTc themedImage--light_HNdA"><img src="/img/knife4j-dark.svg" alt="" class="themedImage_ToTc themedImage--dark_i4oU"></div><b class="navbar__title text--truncate">Knife4j</b></a><a class="navbar__item navbar__link" href="/docs/quick-start">文档</a><a class="navbar__item navbar__link changelog" href="/docs/middleware-sources">中间件</a><a class="navbar__item navbar__link changelog" href="/docs/oas">OAS规范</a><a aria-current="page" class="navbar__item navbar__link changelog navbar__link--active" href="/docs/action">实战指南</a><a class="navbar__item navbar__link changelog" href="/docs/changelog">更新日志</a><a class="navbar__item navbar__link changelog" href="/docs/faq">FAQ</a><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">旧版本</a><ul class="dropdown__menu"><li><a class="dropdown__link" target="_blank" href="/v2/index.html">2.0.9</a></li></ul></div></div><div class="navbar__items navbar__items--right"><div class="toggle_MW0i colorModeToggle_x44X"><button class="clean-btn toggleButton_yw5v toggleButtonDisabled_BJd7" type="button" disabled="" title="切换浅色/暗黑模式（当前为暗黑模式）" aria-label="切换浅色/暗黑模式（当前为暗黑模式）"><span><svg viewBox="0 0 13 12" fill="none" xmlns="http://www.w3.org/2000/svg" width="14" height="14" class="lightToggleIcon_SFTY"><g clip-path="url(#clip0_833_8168)"><path d="M6.59998 8.49999C7.98069 8.49999 9.09998 7.3807 9.09998 5.99999C9.09998 4.61928 7.98069 3.49999 6.59998 3.49999C5.21926 3.49999 4.09998 4.61928 4.09998 5.99999C4.09998 7.3807 5.21926 8.49999 6.59998 8.49999Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M6.59985 0.5V1.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M6.59985 10.5V11.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M2.7099 2.11L3.4199 2.82" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M9.77991 9.17999L10.4899 9.88999" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M1.09998 6H2.09998" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M11.0999 6H12.0999" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M2.7099 9.88999L3.4199 9.17999" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M9.77991 2.82L10.4899 2.11" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path></g><defs><clipPath id="clip0_833_8168"><rect width="12" height="12" fill="white" transform="translate(0.599976)"></rect></clipPath></defs></svg><svg viewBox="0 0 13 12" width="14" height="14" class="darkToggleIcon_ekgs"><path d="M10.7001 6.39501C10.6215 7.24611 10.3021 8.05721 9.77927 8.7334C9.25646 9.40959 8.55189 9.92291 7.748 10.2133C6.9441 10.5036 6.07414 10.5591 5.2399 10.3731C4.40565 10.187 3.64164 9.76728 3.03726 9.1629C2.43287 8.55851 2.01312 7.7945 1.8271 6.96026C1.64108 6.12602 1.6965 5.25605 1.98688 4.45216C2.27725 3.64826 2.79056 2.94369 3.46675 2.42088C4.14294 1.89808 4.95404 1.57866 5.80515 1.50001C5.30685 2.17414 5.06707 3.00473 5.12941 3.84071C5.19175 4.6767 5.55208 5.46254 6.14485 6.05531C6.73762 6.64808 7.52346 7.0084 8.35944 7.07074C9.19542 7.13308 10.026 6.8933 10.7001 6.39501Z" stroke="currentColor" fill="transparent" stroke-linecap="round" stroke-linejoin="round"></path></svg></span></button></div><a href="https://github.com/xiaoymin/swagger-bootstrap-ui" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link icon" title="View on GitHub">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a><div class="searchBox_ZlJk"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">搜索</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper mainWrapper_z2l0 docsWrapper_BCFX"><button aria-label="回到顶部" class="clean-btn theme-back-to-top-button backToTopButton_sjWU" type="button"></button><div class="docPage__5DB"><aside class="theme-doc-sidebar-container docSidebarContainer_b6E3"><div class="sidebar_njMd sidebarWithHideableNavbar_wUlq"><a tabindex="-1" class="sidebarLogo_isFc" href="/"><img src="/img/knife4j-light.svg" alt="" class="themedImage_ToTc themedImage--light_HNdA"><img src="/img/knife4j-dark.svg" alt="" class="themedImage_ToTc themedImage--dark_i4oU"><b>Knife4j</b></a><nav class="menu thin-scrollbar menu_SIkG"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menuHtmlItem_M9Kj menu__list-item">
<div class="sidebar_donate">
    <div class="sidebar_sponsor">
        <a target="_blank" href="https://www.apifox.com/apiskills/apifox-auto-generates-api-docs/?utm_source=pay&utm_medium=knife4jg"><img src="/images/sponsor/apifox/Apifox IDEA@3x.png">
        </a>
    </div>
    <div class="sidebar_sponsor_submit">
        <a href="/docs/community/donate">成为赞助商</a>
    </div>
<div>
</div></div></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="/docs/action">实战指南</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="/docs/action/action-simple">示例代码</a></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/action/mavenbom">Spring 单体架构</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/action/springcloud-gateway">Spring微服务架构</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/action/oauth2-implicit">OAuth2.0</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/action/aggregation-disk">微服务聚合实战</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/action/dotnetcore-knife4j-how">ASP.NET Core</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--active" aria-expanded="true" href="/docs/action/springfox">Springfox 源码系列</a><button aria-label="打开/收起侧边栏菜单「Springfox 源码系列」" type="button" class="clean-btn menu__caret"></button></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox1">springfox 源码分析(一) 程序入口</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox2">springfox 源码分析(二) 初探mapstruct</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox3">springfox 源码分析(三) 初探Spring Plugin插件系统</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox4">springfox 源码分析(四) 配置类初始化</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox5">springfox 源码分析(五) web配置类Plugin插件的使用</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox6">springfox 源码分析(六) web配置类扫描包作用探索</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox7">springfox 源码分析(七) 文档初始化</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/docs/action/springfox/springfox8">springfox 源码分析(八) 遍历接口获取Model对象</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox9">springfox 源码分析(九) 文档初始化-分组</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox10">springfox 源码分析(十) 遍历接口获取Model对象</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox11">springfox 源码分析(十一) 自定义添加Swagger Models功能实现</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox12">springfox 源码分析(十二) 遍历接口获取ApiDescription集合</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox13">springfox 源码分析(十三) 自定义扩展实现接口的排序</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox14">springfox 源码分析(十四) 归档得到ApiListing接口集合</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox15">springfox 源码分析(十五) 归档得到Documentation文档对象</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox16">springfox 源码分析(十六) 分组接口swagger-resouces</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox17">springfox 源码分析(十七) Swagger2接口文档示例接口api-docs</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox18">springfox 源码分析(十八) 自定义扩展实现分组的排序</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox19">springfox 源码分析(十九) guava库学习</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox20">springfox 源码分析(二十) 自定义扩展实现Map、JSONObject等动态字段显示</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/action/springfox/springfox21">springfox 源码分析(二十一) 忽略参数Class类型</a></li></ul></li></ul></nav><button type="button" title="收起侧边栏" aria-label="收起侧边栏" class="button button--secondary button--outline collapseSidebarButton_PEFL"><svg width="20" height="20" aria-hidden="true" class="collapseSidebarButtonIcon_kv0_"><g fill="#7a7a7a"><path d="M9.992 10.023c0 .2-.062.399-.172.547l-4.996 7.492a.982.982 0 01-.828.454H1c-.55 0-1-.453-1-1 0-.2.059-.403.168-.551l4.629-6.942L.168 3.078A.939.939 0 010 2.528c0-.548.45-.997 1-.997h2.996c.352 0 .649.18.828.45L9.82 9.472c.11.148.172.347.172.55zm0 0"></path><path d="M19.98 10.023c0 .2-.058.399-.168.547l-4.996 7.492a.987.987 0 01-.828.454h-3c-.547 0-.996-.453-.996-1 0-.2.059-.403.168-.551l4.625-6.942-4.625-6.945a.939.939 0 01-.168-.55 1 1 0 01.996-.997h3c.348 0 .649.18.828.45l4.996 7.492c.11.148.168.347.168.55zm0 0"></path></g></svg></button></div></aside><main class="docMainContainer_gTbr"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_z5aJ"><div class="docItemContainer_c0TR"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Z_bl" aria-label="页面路径"><ul class="breadcrumbs" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li class="breadcrumbs__item"><a aria-label="主页面" class="breadcrumbs__link" href="/"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_OVgt"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item"><a class="breadcrumbs__link" itemprop="item" href="/docs/action/springfox"><span itemprop="name">Springfox 源码系列</span></a><meta itemprop="position" content="1"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link" itemprop="name">springfox 源码分析(八) 遍历接口获取Model对象</span><meta itemprop="position" content="2"></li></ul></nav><div class="tocCollapsible_ETCw theme-doc-toc-mobile tocMobile_ITEo"><button type="button" class="clean-btn tocCollapsibleButton_TO0P">本页总览</button></div><div class="theme-doc-markdown markdown"><h1>springfox 源码分析(八) 遍历接口获取Model对象</h1><p>我们通过读<code>DocumentationPluginsBootstrapper</code>代码中的<code>start</code>方法,了解到springfox根据我们外部提供的Docket对象进行初始化时,会通过Docket对象构建DocumentationContext对象来进行初始化操作</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">private DocumentationContext buildContext(DocumentationPlugin each) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return each.configure(defaultContextBuilder(each));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p><code>BuildContext</code>方法是通过Docket对象来构建最终的DocumentaionContext对象</p><p>所以我们在研究Documentation之前,先来看DocumentationContext对象主要是包含哪些属性和方法</p><p>先来看<code>DocumentationContext</code>的源码：</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public class DocumentationContext {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //文档类型</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final DocumentationType documentationType;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //请求接口</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;RequestHandler&gt; handlerMappings;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口信息，包括title、描述等信息</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final ApiInfo apiInfo;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //分组名称</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final String groupName;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口选择器</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final ApiSelector apiSelector;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final AlternateTypeProvider alternateTypeProvider;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //忽略的参数类型</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;Class&gt; ignorableParameterTypes;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //请求方法对应的响应状态码信息</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Map&lt;RequestMethod, List&lt;ResponseMessage&gt;&gt; globalResponseMessages;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //全局参数</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;Parameter&gt; globalOperationParameters;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //分组策略</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final ResourceGroupingStrategy resourceGroupingStrategy;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //路径Provider</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final PathProvider pathProvider;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //安全信息</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;SecurityContext&gt; securityContexts;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //安全Scheme</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;? extends SecurityScheme&gt; securitySchemes;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口信息</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Ordering&lt;ApiListingReference&gt; listingReferenceOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口描述</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Ordering&lt;ApiDescription&gt; apiDescriptionOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口信息</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Ordering&lt;Operation&gt; operationOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final GenericTypeNamingStrategy genericsNamingStrategy;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Optional&lt;String&gt; pathMapping;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;ResolvedType&gt; additionalModels;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //tag分组标签</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;Tag&gt; tags;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Set&lt;String&gt; produces;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Set&lt;String&gt; consumes;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //主机号</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private String host;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //协议</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Set&lt;String&gt; protocols;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private boolean isUriTemplatesEnabled;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //扩展属性</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private List&lt;VendorExtension&gt; vendorExtensions;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //getter and setter and constructor</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>我们姑且称他为文档上下文环境吧,springfox是通过文档上下文(DocumentationContext)最终构建真正的Documenation对象,然后缓存在内存中,最终通过接口<code>/v2/api-docs</code>将Documentation对象转换为标准的Swagger对象输出.</p><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="documentationcontextbuilder">DocumentationContextBuilder<a class="hash-link" href="#documentationcontextbuilder" title="标题的直接链接">​</a></h2><p>在springfox的源码中,大量的使用了Builder构造器来进行目标对象的构建,所以文档上下文也一样,最终通过DocumentaionContextBuilder来构造创建</p><p><code>DocumentationPlugin</code>中提供了根据<code>DocumentationContextBuilder</code>来创建DocumenationContext的方法</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public interface DocumentationPlugin extends Plugin&lt;DocumentationType&gt; {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  /**</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * Creates a documentation context based on a given DocumentationContextBuilder</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   *</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * @param builder - @see springfox.documentation.spi.service.contexts.DocumentationContextBuilder</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * @return context to use for building the documentation</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   */</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  DocumentationContext configure(DocumentationContextBuilder builder);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="源码">源码<a class="hash-link" href="#源码" title="标题的直接链接">​</a></h3><p>根据DocumentaionPlugin对象来构建Builder</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public class DocumentationContextBuilder {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //安全参数</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;SecurityContext&gt; securityContexts = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //忽律类型</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;Class&gt; ignorableParameterTypes = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口响应状态码</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Map&lt;RequestMethod, List&lt;ResponseMessage&gt;&gt; responseMessageOverrides = newTreeMap();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //全局参数</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;Parameter&gt; globalOperationParameters = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //类型规则</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;AlternateTypeRule&gt; rules = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //默认接口状态响应吗</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Map&lt;RequestMethod, List&lt;ResponseMessage&gt;&gt; defaultResponseMessages = newHashMap();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //protocols</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;String&gt; protocols = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;String&gt; produces = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;String&gt; consumes = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //扩展类型</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;ResolvedType&gt; additionalModels = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //分组tag</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;Tag&gt; tags = newTreeSet(Tags.tagComparator());</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //扩展属性</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private List&lt;VendorExtension&gt; vendorExtensions = new ArrayList&lt;VendorExtension&gt;();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //类型处理器</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private TypeResolver typeResolver;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口集合</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private List&lt;RequestHandler&gt; handlerMappings;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口信息</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private ApiInfo apiInfo;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //分组名称</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private String groupName;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //资源分组策略</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private ResourceGroupingStrategy resourceGroupingStrategy;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //路径处理</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private PathProvider pathProvider;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private List&lt;? extends SecurityScheme&gt; securitySchemes;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Ordering&lt;ApiListingReference&gt; listingReferenceOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Ordering&lt;ApiDescription&gt; apiDescriptionOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //swagger文档类型</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private DocumentationType documentationType;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Ordering&lt;Operation&gt; operationOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private boolean applyDefaultResponseMessages;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口选择器</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private ApiSelector apiSelector = ApiSelector.DEFAULT;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //主机</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private String host;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //默认类型名称策略</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private GenericTypeNamingStrategy genericsNamingStrategy;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  //接口路径映射</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Optional&lt;String&gt; pathMapping;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private boolean isUrlTemplatesEnabled;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p><code>DocumentationContextBuilder</code>基本覆盖了<code>DocumentationContext</code>的所有属性,而<code>DocumentationContextBuilder</code>没有提供构造函数来实例化参数，只提供了一个构造函数(文档类型),其余参数都是通过Builder构造器模式来赋值属性,最终通过Builder()方法来构建输出DocumentaionContext</p><p><strong>构造函数</strong></p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public DocumentationContextBuilder(DocumentationType documentationType) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    this.documentationType = documentationType;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p><strong>赋值属性</strong></p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public DocumentationContextBuilder requestHandlers(List&lt;RequestHandler&gt; handlerMappings) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    this.handlerMappings = handlerMappings;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return this;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">//more...</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>通过返回this对象的方式,提供了很多属性的赋值方法</p><p><strong>build构造</strong></p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public DocumentationContext build() {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    Map&lt;RequestMethod, List&lt;ResponseMessage&gt;&gt; responseMessages = aggregateResponseMessages();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    OrderComparator.sort(rules);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return new DocumentationContext(documentationType,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        handlerMappings,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        apiInfo,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        groupName,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        apiSelector,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        ignorableParameterTypes,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        responseMessages,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        globalOperationParameters,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        resourceGroupingStrategy,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        pathProvider,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        securityContexts,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        securitySchemes,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        rules,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        listingReferenceOrdering,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        apiDescriptionOrdering,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        operationOrdering,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        produces,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        consumes,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        host,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        protocols,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        genericsNamingStrategy,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        pathMapping,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        isUrlTemplatesEnabled,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        additionalModels,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        tags,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        vendorExtensions);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  }</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>最终通过使用build()方法,调用<code>DocumentationContext</code>的构造函数,构建<code>DocumentationContext</code>对象</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="构造">构造<a class="hash-link" href="#构造" title="标题的直接链接">​</a></h3><p>所以,我们先来看<code>DocumentationContextBuilder</code>对象的创建,主要包含了那些参数、方法</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">/***</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * 构建文档builder</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * @param plugin</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * @return</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   */</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private DocumentationContextBuilder defaultContextBuilder(DocumentationPlugin plugin) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    DocumentationType documentationType = plugin.getDocumentationType();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    //获取RequestHandler</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    //疑问：handlerProviders在何时初始化</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    List&lt;RequestHandler&gt; requestHandlers = from(handlerProviders)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .transformAndConcat(handlers())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .toList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    List&lt;AlternateTypeRule&gt; rules = from(nullToEmptyList(typeConventions))</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          .transformAndConcat(toRules())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">          .toList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return documentationPluginsManager</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .createContextBuilder(documentationType, defaultConfiguration)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .rules(rules)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .requestHandlers(combiner().combine(requestHandlers));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  }</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>通过代码我们可以了解到：</p><ul><li>首先获取所有的RequestHnadler，而RequestHandlerProvider的默认实现类是<code>WebMvcRequestHandlerProvider</code>,该实现类会接收Spring中的所有请求Mapping，最终转化为<code>WebMvcRequestHandler</code>,WebMvcRequestHandler是接口<code>RequestHnadler</code>的实现，这等于是拿到了所有的接口</li><li>获取AlternateTypeRule的规则列表</li><li>初始化创建Builder,赋值请求接口、rules</li></ul><p>了解了<code>DocumentationContextBuilder</code>的构造方式,在来看他的创建过程</p><p>通过代码知道是通过<code>DocumentationPluginsManager</code>的<code>createContextBuilder</code>方法来构建</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public DocumentationContextBuilder createContextBuilder(</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    DocumentationType documentationType,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    DefaultConfiguration defaultConfiguration) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return defaultsProviders.getPluginFor(documentationType, defaultConfiguration)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .create(documentationType)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .withResourceGroupingStrategy(resourceGroupingStrategy(documentationType));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>defaultsProviders是也是一个Plugin接口,但是我们在前面章节也介绍说过,他只有一个实现类<code>DefaultConfiguration</code>,但是该实现类并没有通过<code>@Compoent</code>注解注入到Spring的容器中,所以此处通过Plugin的实现给了一个默认值defaultConfiguration，其实,此处就是使用的defaultConfiguration</p><p>那么该默认配置的create方法做了那些操作呢?,继续跟踪代码：</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public class DefaultConfiguration implements DefaultsProviderPlugin {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    private final Defaults defaults;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    private final TypeResolver typeResolver;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    private final ServletContext servletContext;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    public DefaultConfiguration(Defaults defaults,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                TypeResolver typeResolver,</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">                                ServletContext servletContext) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        this.servletContext = servletContext;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        this.defaults = defaults;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        this.typeResolver = typeResolver;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    @Override</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    public DocumentationContextBuilder create(DocumentationType documentationType) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        return new DocumentationContextBuilder(documentationType)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .operationOrdering(defaults.operationOrdering())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .apiDescriptionOrdering(defaults.apiDescriptionOrdering())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .apiListingReferenceOrdering(defaults.apiListingReferenceOrdering())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .additionalIgnorableTypes(defaults.defaultIgnorableParameterTypes())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .rules(defaults.defaultRules(typeResolver))</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .defaultResponseMessages(defaults.defaultResponseMessages())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .pathProvider(new RelativePathProvider(servletContext))</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .typeResolver(typeResolver)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .enableUrlTemplating(false)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">            .selector(ApiSelector.DEFAULT);</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>主要是给<code>DocumentationContextBuilder</code>赋值了默认的相关参数，主要包括：</p><ul><li>默认忽略Class类型</li><li>默认响应状态码消息</li><li>类型解析器</li><li>接口选择器</li><li>接口排序</li><li>...</li></ul><p>通过上面源码我们知道：</p><ul><li><p>首先defaultsProviders是也是一个Plugin接口,但是我们在前面章节也介绍说过,他只有一个实现类<code>DefaultConfiguration</code>,但是该实现类并没有通过<code>@Compoent</code>注解注入到Spring的容器中,所以此处通过Plugin的实现给了一个默认值defaultConfiguration，其实,此处就是使用的defaultConfiguration</p></li><li><p>defaultConfiguration是在<code>DocumentationPluginsBootstrapper</code>的构造函数中通过new的方式进行构造的</p></li><li><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">//通过DefaultConfiguration可以构建DocumentationContextBuilder</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">this.defaultConfiguration = new DefaultConfiguration(defaults, typeResolver, servletContext);</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li><li><p>在此处的构建过程中，赋值了资源分组策略.</p></li></ul><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="赋值">赋值<a class="hash-link" href="#赋值" title="标题的直接链接">​</a></h3><p>从源码过程中,我们已经了解到Builder赋值的参数主要包括：</p><ul><li>Spring环境中的所有接口,最终是RequestHandler的集合，实际则是<code>WebMvcRequestHandler</code>的集合</li><li>赋值AlternateTypeRule规则集合</li><li>赋值分组策略属性(resourceGroupingStrategy),默认是<code>ClassOrApiAnnotationResourceGrouping</code>实现</li></ul><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="documentationcontext">DocumentationContext<a class="hash-link" href="#documentationcontext" title="标题的直接链接">​</a></h2><p>获取到了DocumentationContextBuilder对象,此时在通过<code>DocumentationPlugin</code>的configure方法,构建<code>DocumentationContext</code></p><p>来看Docket的configure方法</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">/**</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * Builds the Docket by merging/overlaying user specified values.</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * It is not necessary to call this method when defined as a spring bean.</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * NOTE: Calling this method more than once has no effect.</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   *</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   * @see DocumentationPluginsBootstrapper</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">   */</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  public DocumentationContext configure(DocumentationContextBuilder builder) {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return builder</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .apiInfo(apiInfo)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .selector(apiSelector)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .applyDefaultResponseMessages(applyDefaultResponseMessages)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .additionalResponseMessages(responseMessages)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .additionalOperationParameters(globalOperationParameters)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .additionalIgnorableTypes(ignorableParameterTypes)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .ruleBuilders(ruleBuilders)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .groupName(groupName)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .pathProvider(pathProvider)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .securityContexts(securityContexts)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .securitySchemes(securitySchemes)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .apiListingReferenceOrdering(apiListingReferenceOrdering)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .apiDescriptionOrdering(apiDescriptionOrdering)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .operationOrdering(operationOrdering)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .produces(produces)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .consumes(consumes)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .host(host)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .protocols(protocols)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .genericsNaming(genericsNamingStrategy)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .pathMapping(pathMapping)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .enableUrlTemplating(enableUrlTemplating)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .additionalModels(additionalModels)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .tags(tags)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .vendorExtentions(vendorExtensions)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .build();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  }</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>针对Builder的一些列二次赋值,最终通过build方法构造</p><p>我们的Docket对象是我们开发人员在外部通过Bean来创建的,来看Docket的部分代码：</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">public class Docket implements DocumentationPlugin {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  public static final String DEFAULT_GROUP_NAME = &quot;default&quot;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final DocumentationType documentationType;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;SecurityContext&gt; securityContexts = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Map&lt;RequestMethod, List&lt;ResponseMessage&gt;&gt; responseMessages = newHashMap();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;Parameter&gt; globalOperationParameters = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final List&lt;Function&lt;TypeResolver, AlternateTypeRule&gt;&gt; ruleBuilders = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;Class&gt; ignorableParameterTypes = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;String&gt; protocols = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;String&gt; produces = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;String&gt; consumes = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;ResolvedType&gt; additionalModels = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private final Set&lt;Tag&gt; tags = newHashSet();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private PathProvider pathProvider;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private List&lt;? extends SecurityScheme&gt; securitySchemes;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Ordering&lt;ApiListingReference&gt; apiListingReferenceOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Ordering&lt;ApiDescription&gt; apiDescriptionOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Ordering&lt;Operation&gt; operationOrdering;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private ApiInfo apiInfo = ApiInfo.DEFAULT;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private String groupName = DEFAULT_GROUP_NAME;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private boolean enabled = true;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private GenericTypeNamingStrategy genericsNamingStrategy = new DefaultGenericTypeNamingStrategy();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private boolean applyDefaultResponseMessages = true;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private String host = &quot;&quot;;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private Optional&lt;String&gt; pathMapping = Optional.absent();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private ApiSelector apiSelector = ApiSelector.DEFAULT;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private boolean enableUrlTemplating = false;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">  private List&lt;VendorExtension&gt; vendorExtensions = newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>我们通过Docket来外部赋值的对象值,最终都会构建到<code>DocumentationContext</code>上下文中，我们先来看我们开发中一般创建Docket对象过程</p><div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#F8F8F2"><span class="token plain">@Bean(value = &quot;defaultApi&quot;)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">@Order(value = 4)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">public Docket defaultApi() {</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    ParameterBuilder parameterBuilder=new ParameterBuilder();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    List&lt;Parameter&gt; parameters= Lists.newArrayList();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    parameterBuilder.name(&quot;token&quot;).description(&quot;token令牌&quot;).modelRef(new ModelRef(&quot;String&quot;))</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .parameterType(&quot;header&quot;)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .required(true).build();</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    parameters.add(parameterBuilder.build());</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    Docket docket=new Docket(DocumentationType.SWAGGER_2)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .apiInfo(apiInfo())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .groupName(&quot;默认接口&quot;)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .select()</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .apis(RequestHandlerSelectors.basePackage(&quot;com.swagger.bootstrap.ui.demo.controller&quot;))</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .paths(PathSelectors.any())</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .build().globalOperationParameters(parameters)</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">        .securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.&lt;SecurityScheme&gt;newArrayList(apiKey()));</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">    return docket;</span><br></span><span class="token-line" style="color:#F8F8F2"><span class="token plain">}</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg class="copyButtonIcon_y97N" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_LjdS" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>一般创建Docket对象,主要的赋值参数：</p><ul><li>分组名称</li><li>apiInfo信息</li><li>ApiSelector</li><li>全局参数</li><li>权限验证</li></ul><p>主要包含了以上的一些信息，整个文档上下文环境构造完成,我们也可以以Debug的方式来跟踪代码,如下图：</p><p><img loading="lazy" src="/assets/images/init-8d0e9003ee3e15499f95ea44c6684328.png" width="1657" height="834" class="img_ev3q"></p><p>除了默认的参数赋值外,接口、definitions、model等关键信息等都还没有初始化。</p></div><footer class="theme-doc-footer docusaurus-mt-lg"><div class="theme-doc-footer-edit-meta-row row"><div class="col"></div><div class="col lastUpdated_vwxv"><span class="theme-last-updated">最后<!-- -->由 <b>xiaoyumin</b> <!-- -->于 <b><time datetime="2022-08-15T15:32:11.000Z">2022年8月15日</time></b> <!-- -->更新</span></div></div></footer></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="文档分页导航"><a class="pagination-nav__link pagination-nav__link--prev" href="/docs/action/springfox/springfox7"><div class="pagination-nav__sublabel">上一页</div><div class="pagination-nav__label">springfox 源码分析(七) 文档初始化</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/docs/action/springfox/springfox9"><div class="pagination-nav__sublabel">下一页</div><div class="pagination-nav__label">springfox 源码分析(九) 文档初始化-分组</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#documentationcontextbuilder" class="table-of-contents__link toc-highlight">DocumentationContextBuilder</a><ul><li><a href="#源码" class="table-of-contents__link toc-highlight">源码</a></li><li><a href="#构造" class="table-of-contents__link toc-highlight">构造</a></li><li><a href="#赋值" class="table-of-contents__link toc-highlight">赋值</a></li></ul></li><li><a href="#documentationcontext" class="table-of-contents__link toc-highlight">DocumentationContext</a></li></ul></div></div></div></div></main></div></div><footer class="footer"><div class="container container-fluid"><div class="row footer__links"><div class="col footer__col"><div class="footer__title">文档指南</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/docs/quick-start">文档</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/community/simple-demo">示例</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/changelog">更新日志</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/upgrading/upgrading-to-v4">升级到v4.0</a></li></ul></div><div class="col footer__col"><div class="footer__title">社区</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://gitter.im/knife4j/knife4j" target="_blank" rel="noopener noreferrer" class="footer__link-item">Gitter<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://www.oschina.net/question/tag/swagger-bootstrap-ui" target="_blank" rel="noopener noreferrer" class="footer__link-item">开源中国<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><div class="col footer__col"><div class="footer__title">关注公众号</div><ul class="footer__items clean-list"><li class="footer__item"><img src="/images/website/qrcode.jpg" width="150"></li></ul></div><div class="col footer__col"><div class="footer__title">更多</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://docusaurus.io" target="_blank" rel="noopener noreferrer" class="footer__link-item">Docusaurus<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://gitee.com/xiaoym/knife4j" target="_blank" rel="noopener noreferrer" class="footer__link-item">Gitee<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://github.com/xiaoymin/swagger-bootstrap-ui" target="_blank" rel="noopener noreferrer" class="footer__link-item">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div></div><div class="footer__bottom text--center"><div class="footer__copyright">Apache License 2.0 | Copyright © 2018-2023-八一菜刀 浙ICP备18027673号-1 </div></div></div></footer></div>
<script src="/assets/js/runtime~main.e0db172c.js"></script>
<script src="/assets/js/main.c059fb6e.js"></script>
<!-- Google Tag Manager (noscript) -->
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TKBX678" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!-- End Google Tag Manager (noscript) --></body>
</html>